package com.x.client.activity

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.gg.baselibrary.BaseActivity
import com.gg.utils.AppManager
import com.gg.utils.toast
import com.gg.widget.bottomnavygation.BottomNav
import com.gg.widget.bottomnavygation.ItemNav
import com.x.client.R
import com.x.client.fragment.BuyCarFragment
import com.x.client.fragment.HomeFragment
import com.x.client.fragment.MyFragment
import com.x.client.fragment.SaleCarFragment
import com.gg.utils.StatusUtils
import com.x.client.utils.RouterClass
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    private val mFragments: ArrayList<Fragment> by lazy { ArrayList<Fragment>() }

    private var mExitTime: Long = 0

    private val EXIT_TIME: Long = 2000

    override fun getLayoutRes(): Int = R.layout.activity_main

    override fun initView() {
        StatusUtils.setStatusTextColor(true, this@MainActivity)
        StatusUtils.setStatusBar(this@MainActivity, false, false)
        createTab()
        initFragment()
        bottomBar.setTabSelectedListener(object : BottomNav.OnTabSelectedListener {
            override fun onTabLongSelected(p0: Int) {

            }

            override fun onTabSelected(p0: Int) {
                viewPager.currentItem = p0
            }

        })
    }

    private fun initFragment() {
        mFragments.add(HomeFragment.newInstance())
        mFragments.add(BuyCarFragment.newInstance())
        mFragments.add(SaleCarFragment.newInstance())
        mFragments.add(MyFragment.newInstance())


        viewPager.offscreenPageLimit = mFragments.size
        viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = mFragments[position]

            override fun getCount(): Int = mFragments.size

        }
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
//                KLog.w("positionOffset", positionOffset)
//                KLog.w("positionOffsetPixels", positionOffsetPixels)
//                KLog.w("position", position)
            }

            override fun onPageSelected(position: Int) {
                bottomBar.selectTab(position)
                setFlatBtn(position)
            }
        })
        setFlatBtn(0)
    }

    private fun setFlatBtn(position: Int) {
        when (position) {
            0 -> {
                with(flatButton) {
                    setImageResource(R.drawable.ic_subscribe_button)
                    visibility = View.VISIBLE

                    setOnClickListener {
                        ARouter.getInstance()
                                .build(RouterClass.carSubscribe)
                                .navigation()
                    }
                }
            }
            1, 3 -> {
                with(flatButton) {
                    visibility = View.GONE
                }
            }
            2 -> {
                with(flatButton) {
                    setImageResource(R.drawable.ic_sale_car)
                    visibility = View.VISIBLE
                    setOnClickListener {
                        ARouter.getInstance()
                                .build(RouterClass.saleCar)
                                .navigation()
                    }
                }
            }

        }
    }

    override fun initData() {
    }

    private fun createTab() {
        bottomBar.addItemNav(ItemNav(this, R.drawable.ic_home_default, R.drawable.ic_home_orange, "首页").addColorAtive(R.color.text_orange).addColorInative(R.color.text_black))
        bottomBar.addItemNav(ItemNav(this, R.drawable.ic_buy_car_default, R.drawable.ic_buy_car_orange, "买车").addColorAtive(R.color.text_orange).addColorInative(R.color.text_black))
        bottomBar.addItemNav(ItemNav(this, R.drawable.ic_sale_car_default, R.drawable.ic_sale_car_orange, "卖车").addColorAtive(R.color.text_orange).addColorInative(R.color.text_black))
        bottomBar.addItemNav(ItemNav(this, R.drawable.ic_my_default, R.drawable.ic_my_orange, "我的").addColorAtive(R.color.text_orange).addColorInative(R.color.text_black))
        bottomBar.build()
    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - mExitTime > EXIT_TIME) {
            toast(R.string.exit_toast)
            mExitTime = System.currentTimeMillis()
        } else {
            AppManager.appExit(this)
        }
    }


}
